using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace CreatingRainbows
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ISpyRoom : GameScene
    {
        SpriteBatch spriteBatch;

        //ISpyObject object1;
        //ISpyObject object2;
        //ISpyObject object3;

        List<ISpyObject> objects;

        Texture2D iSpyImg;

        private MouseState mouseState;
        private MouseState prevMouseState;

        public const int ISPY_OBJECTS = 3;

        public ISpyRoom(Game1 game, Texture2D textureBack, Texture2D textureFront)
            : base(game, false)
        {
            mainReference = game;

            Components.Add(new ImageComponent(game, textureBack, ImageComponent.DrawMode.Stretch));
            Components.Add(new ImageComponent(game, textureFront, ImageComponent.DrawMode.Center));

            Initialize();

            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            objects = new List<ISpyObject>();

            for (int i = 0; i < ISPY_OBJECTS; i++)
            {
                objects.Add(new ISpyObject(mainReference));
                this.Components.Add(objects[i]);
            }
            for (int i = 0; i < objects.Count; i++)
            {
                objects[i].Initialize(Vector2.Zero, new Rectangle(0, 0, 100, 100), Color.White,
                             Vector2.Zero, new Vector2(1, 1), 0.0f);
            }

            #region unused initialize
            ////object1 = new ISpyObject(mainReference);
            ////this.Components.Add(object1);
            //objects[0].Initialize(Vector2.Zero, new Rectangle(0, 0, 100, 100), Color.White,
            //                 Vector2.Zero, new Vector2(1, 1), 0.0f);

            ////object2 = new ISpyObject(mainReference);
            ////this.Components.Add(object2);
            //objects[1].Initialize(Vector2.Zero, new Rectangle(0, 0, 100, 100), Color.White,
            //                 Vector2.Zero, new Vector2(1, 1), 0.0f);

            ////object3 = new ISpyObject(mainReference);
            ////this.Components.Add(object3);
            //objects[2].Initialize(Vector2.Zero, new Rectangle(0, 0, 100, 100), Color.White,
            //                 Vector2.Zero, new Vector2(1, 1), 0.0f);
            #endregion

            // TODO: Add your initialization code here

            base.Initialize();

            LoadContent();


        }

        protected override void LoadContent()
        {
            iSpyImg = mainReference.Content.Load<Texture2D>("iSpyImg");

            for (int i = 0; i < objects.Count; i++)
            {
                objects[i].LoadContent(spriteBatch, ref iSpyImg);
            }
            //object1.LoadContent(spriteBatch, ref iSpyImg);
            //object2.LoadContent(spriteBatch, ref iSpyImg);
            //object3.LoadContent(spriteBatch, ref iSpyImg);

            base.LoadContent();

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)

        {
            // TODO: Add your update code here

            MouseClick();

            if (objects.Count == 0)
            {
                mainReference.MagentaRoomScene.Enterance = 2;
                mainReference.MagentaRoomScene.Goal0 = true;

                mainReference.MagentaRoomScene.CheckGoals();

                this.Hide();
                mainReference.MagentaRoomScene.Show();
                mainReference.MagentaRoomScene.PuzzleDoor = new Door(mainReference, false);

                mainReference.ActiveScene = mainReference.MagentaRoomScene;
            }

            base.Update(gameTime);
        }

        public override void Show()
        {
            objects[0].position = Localize(50, 260);

            objects[1].position = Localize(200, 150);

            objects[2].position = Localize(350, 40);

            #region unused placement code
            //object1.position.X = mainReference.ForegroundX + 50;
            //object1.position.Y = mainReference.ForegroundY + 260;
            //object2.position.X = mainReference.ForegroundX + 200;
            //object2.position.Y = mainReference.ForegroundY + 150;
            //object3.position.X = mainReference.ForegroundX + 350;
            //object3.position.Y = mainReference.ForegroundY + 40;
            #endregion

            base.Show();
        }

        public void MouseClick()
        {
            mouseState = Mouse.GetState();
            if (prevMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released)
            {
                List<ISpyObject> temp = new List<ISpyObject>();
                foreach (ISpyObject x in objects)
                {
                    if(x.MouseOver(mouseState.X, mouseState.Y))
                    {
                        temp.Add(x);
                    }
                }

                foreach (ISpyObject x in temp)
                {
                    x.Dispose();
                    objects.Remove(x);
                    this.Components.Remove(x);
                }
            }
            prevMouseState = mouseState;
        }
    }
}
